import facade
from xjlibrary.configread import MyConfigParser
from xjlibrary.our_file_dir import BaseDir


class FileTools(object):

    def __init__(self):
        self.config_file = ""
        self.inputs1 = []
        self.inputs2 = []
        self.inputobj1 = set()
        self.inputobj2 = set()
        self.output = ""
        self.logger = facade.get_streamlogger()
        self.sqlite3utiles = facade.Sqlite3Utiles(logger=self.logger)
        self.mysqlutiles = None
        self.sql = ""

    def set_config_file(self, configfile):
        self.config_file = configfile

    def conn_sqlite3(self, dbfilepath):
        """
        通过文件连接sqlit3
        """
        return self.sqlite3utiles.Sqlite3DBConnectFromFilePath(dbfilepath)

    def conn_mysql(self, sesc="mysql", builder="MysqlBuilder"):
        self.mysqlutiles = facade.MysqlUtiles(self.config_file, sesc, logger=self.logger, builder=builder)

    def sqlite_select(self):
        """
        sqlite3的select
        :return:
        """
        sql = self.get_sql()
        rows = self.sqlite3utiles.SelectFromSqlliteFetchall(sql)
        return rows

    def mysql_select(self):
        sql = self.get_sql()
        rows = self.mysqlutiles.SelectFromDB(sql)
        return rows

    def get_sql(self):
        """
        这里返回一个需要的sql语句由于每个场景的sql需求
        不一样 故这里不写而是继承该类的类来确定sql语句
        :return:
        """
        return self.sql

    def set_sql(self, sql):
        self.sql = sql

    def deal_sqlite3_select(self, rows):
        """
        对select返回的结果进行处理
        :param rows:
        :return:
        """
        pass

    def deal_mysql_select(self, rows):
        pass

    def get_config_set_value(self):
        assert self.config_file, Exception("config 配置文件不存在")
        dicts = MyConfigParser(self.config_file).set_keep_keys_case().read_config().conf_to_dict()
        if "file_compare" in dicts:
            ftf_dicts = dicts["file_compare"]
            inputs = ftf_dicts["inputs1"]
            self.inputs1.extend(inputs.split(","))
            inputs = ftf_dicts["inputs2"]
            self.inputs2.extend(inputs.split(","))
            output = ftf_dicts["output"]
            self.output = output
        return dicts

    def file_add_set(self, filepathlist, setobj):
        for inputfile in filepathlist:
            for line in BaseDir.read_file_r_mode_yield(inputfile):
                setobj.add(line)

    def set_inputobj1(self):
        self.file_add_set(self.inputs1, self.inputobj1)

    def set_inputobj2(self):
        self.file_add_set(self.inputs2, self.inputobj2)

    def file_compare(self):
        """
        文件对比 求差 求和 求交集
        一定量的对比 一次读入内存
        :return:
        """
        # 差集 self.inputobj1.difference(self.inputobj2)
        return self.inputobj1 - self.inputobj2
        # 交集 self.inputobj1.intersection(self.inputobj2)
        # return self.inputobj1 & self.inputobj2
        # 并集 self.inputobj1.union(self.inputobj2)
        # return self.inputobj1 | self.inputobj2
        # 对称差集(项在t或s中，但不会同时出现在二者中)
        # self.inputobj1.symmetric_difference(self.inputobj2)
        # return self.inputobj1 ^ self.inputobj2

    def file_compare2(self):
        """
        这个比较返回 True or False
        :return:
        """
        # 测试是否 self.inputobj2 中的每一个元素都在 self.inputobj1 中
        # self.inputobj2.issubset(self.inputobj1)
        return self.inputobj2 <= self.inputobj1
        # 测试是否 self.inputobj2 中的每一个元素都在 self.inputobj1 中
        # self.inputobj1.issuperset(self.inputobj2)
        # self.inputobj1 >= self.inputobj2

    def file_to_file(self):
        """
        文件到文件的处理 即需要在每一行上做处理时使用
        :return:
        """
        pass

    def write_output_file(self, value):
        """
        传入的可以是set()
        :param value:
        :return:
        """
        string_temp = '\n'.join(value)
        BaseDir.single_write_file(string_temp, self.output)

    def deal_file_result(self, value):
        pass

    def create_temp_tab(self):
        """
        这个函数被重写用于创建临时表
        :return:
        """
        # sql = "DROP TABLE IF EXISTS `temp`"
        # self.sqlite3utiles.ExeSqlliteSql(sql)
        # CREATE TABLE "temp" ("lngid" TEXT,"rawid" TEXT,"identifier_doi" TEXT,"title" TEXT,"title_sub" TEXT,"title_alternative" TEXT,"title_edition" TEXT,"title_series" TEXT,"identifier_pisbn" TEXT,"identifier_eisbn" TEXT,"identifier_pissn" TEXT,"identifier_eissn" TEXT,"identifier_cnno" TEXT,"identifier_standard" TEXT,"creator" TEXT,"creator_en" TEXT,"creator_bio" TEXT,"creator_institution" TEXT,"creator_discipline" TEXT,"creator_degree" TEXT,"creator_drafting" TEXT,"creator_release" TEXT,"contributor" TEXT,"creator_cluster" TEXT,"agency" TEXT,"gch" TEXT,"source" TEXT,"source_en" TEXT,"source_institution" TEXT,"publisher" TEXT,"volume" TEXT,"issue" TEXT,"bookid" TEXT,"description" TEXT,"description_en" TEXT,"description_unit" TEXT,"description_fund" TEXT,"description_core" TEXT,"description_cycle" TEXT,"description_type" TEXT,"subject" TEXT,"subject_en" TEXT,"provider_subject" TEXT,"page" TEXT,"beginpage" TEXT,"endpage" TEXT,"jumppage" TEXT,"pagecount" TEXT,"subject_clc" TEXT,"subject_esc" TEXT,"subject_dsa" TEXT,"subject_csc" TEXT,"subject_isc" TEXT,"date" TEXT,"date_impl" TEXT,"date_created" TEXT,"pct_enter_nation_date" TEXT,"pct_app_data" TEXT,"pct_pub_data" TEXT,"priority_number" TEXT,"language" TEXT,"country" TEXT,"if_html_fulltext" TEXT,"if_pdf_fulltext" TEXT,"if_pub1st" TEXT,"ref_cnt" TEXT,"cited_cnt" TEXT,"down_cnt" TEXT,"rawtype" TEXT,"provider" TEXT,"provider_url" TEXT,"provider_id" TEXT,"cover" TEXT,"agents" TEXT,"legal_status" TEXT,"province_code" TEXT,"folio_size" TEXT,"price" TEXT,"type" TEXT,"medium" TEXT,"batch" TEXT );
